package com.blogs.ui;



import com.blogs.entity.AppEnums;
import com.blogs.tools.DBSharedPreferences;
import com.cnblogs.R;

import android.app.Application;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TabHost;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;

public class MainTabActivity extends TabActivity{
	
	private static final String TAB_HOME = "tabHome";
	private static final String TAB_NEWS = "tabNews";
	private static final String TAB_SEARCH = "tabSearch";
	private static final String TAB_SETTING = "tabSetting";
	
	private Application app;
	
	
	private TabHost tabHost; 
	private LayoutInflater inflater;
	private ImageView ivStatus;
	private LinearLayout maintab_root;
	
	private FullScreenReceiver fullScreenReceiver;
	private SkinChangeReceiver sinkChangeReceiver;
	protected DBSharedPreferences sp;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.maintab);
		maintab_root=(LinearLayout)findViewById(R.id.maintab_root);
        sp=new DBSharedPreferences(this);
        if(sp.GetIsFullScreen())
		{
			getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
	                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		}
    	app =  this.getApplication();
		inflater = LayoutInflater.from(app);
		fullScreenReceiver=new FullScreenReceiver();
		IntentFilter fullFilter = new IntentFilter();
		fullFilter.addAction("ChangeScreenState");
        registerReceiver(fullScreenReceiver, fullFilter);
        
        sinkChangeReceiver=new SkinChangeReceiver();
        IntentFilter skinFilter = new IntentFilter();
        skinFilter.addAction("SkinChangeReceiver");
        registerReceiver(sinkChangeReceiver, skinFilter);

		
		tabHost = getTabHost();
		setupTab();
		
		
	}
	private void setupTab()
	{
		View view = null;
		TabSpec spec = null;
		ImageView iv=null;
		tabHost.getTabWidget().setBackgroundDrawable(getResources().getDrawable(R.drawable.home_buttom));
		
		
		// home
		view = inflater.inflate(R.layout.tab_indicator, null);
//		ivStatus = (ImageView) view.findViewById(R.id.iv_tab_count);
		iv = (ImageView) view.findViewById(R.id.iv_tab);
		iv.setImageDrawable(getResources().getDrawable(R.drawable.buttomicon_home_show));
		spec = tabHost.newTabSpec(TAB_HOME).setIndicator(view).setContent(new Intent(this, CnblogsMainActivity.class));
		tabHost.addTab(spec);
		
		
		// news
		view = inflater.inflate(R.layout.tab_indicator, null);
//		ivStatus = (ImageView) view.findViewById(R.id.iv_tab_count);
		iv = (ImageView) view.findViewById(R.id.iv_tab);
		iv.setImageDrawable(getResources().getDrawable(R.drawable.buttomicon_news_show));
		spec = tabHost.newTabSpec(TAB_NEWS).setIndicator(view).setContent(new Intent(this, NewsActivity.class));
		tabHost.addTab(spec);
		
		// search
		view = inflater.inflate(R.layout.tab_indicator, null);
//		ivStatus = (ImageView) view.findViewById(R.id.iv_tab_count);
		iv = (ImageView) view.findViewById(R.id.iv_tab);
		iv.setImageDrawable(getResources().getDrawable(R.drawable.buttomicon_search_show));
		spec = tabHost.newTabSpec(TAB_SEARCH).setIndicator(view).setContent(new Intent(this, SearchActivity.class));
		tabHost.addTab(spec);
		
		// setting
		view = inflater.inflate(R.layout.tab_indicator, null);
//		ivStatus = (ImageView) view.findViewById(R.id.iv_tab_count);
		iv = (ImageView) view.findViewById(R.id.iv_tab);
		iv.setImageDrawable(getResources().getDrawable(R.drawable.buttomicon_person_show));
		spec = tabHost.newTabSpec(TAB_SETTING).setIndicator(view).setContent(new Intent(this, SettingActivity.class));
		tabHost.addTab(spec);
		InitSkin();
	}
	private void InitSkin()
	{
		if(sp.GetIsDarkState())
		{
			tabHost.getTabWidget().setBackgroundDrawable(getResources().getDrawable(R.drawable.home_buttom_dark));
			maintab_root.getBackground().setLevel(2);
		}
		else
		{
			tabHost.getTabWidget().setBackgroundDrawable(getResources().getDrawable(R.drawable.home_buttom));
			maintab_root.getBackground().setLevel(1);
		}
	}
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		unregisterReceiver(fullScreenReceiver);   //取消注册
		unregisterReceiver(sinkChangeReceiver);   //取消注册
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if(keyCode==KeyEvent.KEYCODE_MENU)
		{
			tabHost.setCurrentTab(2);
			return true;
		}
		else
		{
			return super.onKeyDown(keyCode, event);
		}
	}
	class SkinChangeReceiver extends BroadcastReceiver{
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			InitSkin();
		}
	}
	class FullScreenReceiver extends BroadcastReceiver{
		private Toast toast; 
	        @Override
	        public void onReceive(Context context, Intent intent) {
	        	int state = intent.getExtras().getInt("state");
	        	if(state==AppEnums.ScreenState.Full)
	        	{
		        	getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,    
			                WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		        	showToast("进入全屏");
	        	}
	        	else
	        	{
	        		getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
	        	}
	        	
	        }
	    	private void showToast(String text)
	    	{
	    		if (toast == null) {
	    			toast = Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT);
	    		} else {
	    			toast.setText(text);
	    			toast.setDuration(Toast.LENGTH_SHORT);
	    		}
	    		toast.show();
	    	}
	 }

}
